<?php

namespace app\api\controller;


use app\common\model\DynamicComment;
use app\common\model\DynamicThumb;
use app\service\DynamicCommentService;
use app\service\DynamicService;

/**
 * Class Dynamic
 * @package app\api\controller
 */
class Dynamic extends Base
{
    public $dynamicModel;
    public $dynamicCommentModel;
    public $dynamicThumbModel;

    public function __construct()
    {
        parent::__construct();
        $this->dynamicModel = new \app\common\model\Dynamic();
        $this->dynamicCommentModel = new DynamicComment();
        $this->dynamicThumbModel = new DynamicThumb();
    }

    /**
     * 动态列表
     * @return \think\response\Json
     */
    public function article_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            $list = DynamicService::S()->queryPage($where, $page, $size, 'a.*,b.nickname,b.headimgurl', 'a.create_time desc', true, $this->uid);
            $total = DynamicService::S()->queryCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    /**
     * 动态添加
     * @return \think\response\Json
     */
    public function article_add()
    {
        if (request()->isPost()) {
            $data = input('');
            $save_data = [
                'create_time' => time(),
                'update_time' => time(),
                'uid' => $this->uid,
                'content' => $data['content'],
                'images' => json_encode($data['images']),
                'type' => $data['type'],
                'video' => $data['video']
            ];
            $res = DynamicService::S()->add($save_data);
            if ($res) {
                return returnJson(200, '动态发布成功', $res);
            } else {
                return returnJson(202, '动态发布失败');
            }
        }
    }

    /**
     * 动态详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function article_detail()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = [];
            if (!$data['id']) {
                return returnJson(202, '动态不存在');
            }
            $where['id'] = $data['id'];
            $article = $this->dynamicModel
                ->alias('a')
                ->join('zkhb_user b', 'a.uid=b.id', 'left')
                ->field('a.*,b.nickname,b.headimgurl')
                ->where($where)
                ->find();
            if ($article['images']) {
                $images = json_decode($article['images'], true);
                foreach ($images as $key => $image) {
                    $images[$key] = tomedia($image);
                }
                $article['images'] = $images;
            }
            if ($article['video']) {
                $article['video'] = tomedia($article['video']);
            }
            if ($article['headimgurl']) {
                $article['headimgurl'] = tomedia($article['headimgurl']);
            }
        }
    }

    /**
     * 评论列表
     * @return \think\response\Json
     */
    public function comment_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            if (!$data['aid']) {
                return returnJson(202, '动态不存在');
            }
            $where['a.a_id'] = $data['aid'];
            $list = DynamicCommentService::S()->queryPage($where, $page, $size);
            $total = DynamicCommentService::S()->queryCount($where);
            return returnJson(200, '', compact('list', 'page', 'size', 'total'));
        }
    }

    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function del_comment()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['id']) {
                return returnJson(202, '评论不存在');
            } else {
                $comment = $this->dynamicCommentModel->where('id', $data['id'])->find()->toArray();
                if ($comment) {
                    $guide = DynamicService::S()->findGuide(['id' => $comment['guide_id']]);
                    if ($comment['uid'] == $this->uid || $guide['uid'] == $this->uid) {
                        $res = $this->dynamicCommentModel->where('id', $data['id'])->delete();
                        if ($res) {
                            return returnJson(200, '删除成功');
                        } else {
                            return returnJson(202, '删除失败');
                        }
                    } else {
                        return returnJson(202, '无权删除评论');
                    }
                } else {
                    return returnJson(202, '评论不存在');
                }
            }
        }
    }

    /**
     * 评论
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function comment_add()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['aid']) {
                return returnJson(202, '动态不存在');
            } else {
                $article = $this->guideArticleModel
                    ->where('id', $data['aid'])
                    ->find();
                if (!$article) {
                    return returnJson(202, '动态不存在');
                }
            }
            if (!$data['content']) {
                return returnJson(200, '请填入评价内容');
            }
            $save_data = [
                'create_time' => time(),
                'update_time' => time(),
                'guide_id' => $article['guide_id'],
                'uid' => $this->uid,
                'content' => $data['content'],
                'a_id' => $data['aid'],
            ];
            $res = DynamicCommentService::S()->add($save_data);
            if ($res) {
                return returnJson(200, '评论发布成功', $res);
            } else {
                return returnJson(202, '评论发布失败');
            }
        }
    }

    /**
     * 点赞列表
     * @return \think\response\Json
     */
    public function thumb_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            if (!$data['aid']) {
                return returnJson(202, '动态不存在');
            }
            $where = [
                'a.a_id' => $data['aid']
            ];
            $list = GuideThumbService::S()->queryPage($where, $page, $size);
            $total = GuideThumbService::S()->queryCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    /**
     * 删除动态
     * @return \think\response\Json
     */
    public function del_article()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $res = $this->guideArticleModel->where('id', $data['id'])->delete();
                $this->dynamicCommentModel->where('a_id', $data['id'])->delete();
                $this->guideThumbModel->where('a_id', $data['id'])->delete();
                if ($res) {
                    return returnJson(200, '删除成功');
                } else {
                    return returnJson(202, '删除失败');
                }
            } else {
                return returnJson(202, '动态不存在');
            }
        }
    }

    /**
     * 点赞/取消点赞
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function thumb_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['aid']) {
                $save = [
                    'uid' => $this->uid,
                    'a_id' => $data['aid']
                ];
                $thumb = $this->guideThumbModel->where($save)->find();
                if ($thumb) {
                    $res = $this->guideThumbModel->where($save)->delete();
                    if ($res) {
                        return returnJson(200, '取消点赞');
                    } else {
                        return returnJson(202, '取消点赞失败');
                    }
                } else {
                    $save['create_time'] = time();
                    $save['update_time'] = time();
                    $save['thumb_time'] = time();
                    $res = $this->guideThumbModel->insertGetId($save);
                    if ($res) {
                        return returnJson(200, '点赞成功', $res);
                    } else {
                        return returnJson(202, '点赞失败');
                    }
                }
            } else {
                return returnJson(202, '动态不存在');
            }
        }
    }

}